﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Office = Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

// TODO:  Follow these steps to enable the Ribbon (XML) item:

// 1: Copy the following code block into the ThisAddin, ThisWorkbook, or ThisDocument class.

//  protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
//  {
//      return new Ribbon1();
//  }

// 2. Create callback methods in the "Ribbon Callbacks" region of this class to handle user
//    actions, such as clicking a button. Note: if you have exported this Ribbon from the Ribbon designer,
//    move your code from the event handlers to the callback methods and modify the code to work with the
//    Ribbon extensibility (RibbonX) programming model.

// 3. Assign attributes to the control tags in the Ribbon XML file to identify the appropriate callback methods in your code.  

// For more information, see the Ribbon XML documentation in the Visual Studio Tools for Office Help.


namespace LightingTemplate
{
    [ComVisible(true)]
    public class Ribbon1 : Office.IRibbonExtensibility
    {
        private Office.IRibbonUI ribbon;
        private const string SchemaNameSpace = "http://www.aurorachina.com.cn/";

        public Ribbon1()
        {
        }

        #region IRibbonExtensibility Members

        public string GetCustomUI(string ribbonID)
        {
            return GetResourceText("LightingTemplate.Ribbon1.xml");
        }

        #endregion

        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1

        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            this.ribbon = ribbonUI;
        }

        #endregion

        #region Helpers

        private static string GetResourceText(string resourceName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            string[] resourceNames = asm.GetManifestResourceNames();
            for (int i = 0; i < resourceNames.Length; ++i)
            {
                if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
                {
                    using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
                    {
                        if (resourceReader != null)
                        {
                            return resourceReader.ReadToEnd();
                        }
                    }
                }
            }
            return null;
        }

        #endregion

        /// <summary>
        /// 处理数据源导入按钮点击事件
        /// </summary>
        /// <param name="ribbonControl"></param>
        public void OnImportBtnClicked(Office.IRibbonControl ribbonControl)
        {
            try
            {
                Excel.Workbook workBook = Globals.ThisAddIn.Application.ActiveWorkbook;
                if (workBook.XmlMaps.Count > 0)
                {
                    foreach (Excel.XmlMap xmlMap in workBook.XmlMaps)
                    {
                        if (xmlMap.Name == "灯具清单")
                        {
                            MessageBox.Show("文档中已经导入了表的结构。", "提示信息", MessageBoxButtons.OK);
                            return;
                        }
                    }
                }

                string schema = GetResourceText("LightingTemplate.SchemaList.xsd");
                workBook.XmlMaps.Add(schema, "灯具的相关表").Name = "灯具清单";

                schema = GetResourceText("LightingTemplate.SchemaAllSpec.xsd");
                workBook.XmlMaps.Add(schema, "灯具的相关表").Name = "SPEC完全版打印";
                MessageBox.Show("灯具的表结构导入成功。", "提示信息", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
            }
        }

        public void OnImportBtnClicked1(Office.IRibbonControl ribbonControl)
        {
            try
            {
                Excel.Workbook workBook = Globals.ThisAddIn.Application.ActiveWorkbook;
                StringBuilder sb = new StringBuilder();
                foreach (Excel.Worksheet sheet in workBook.Sheets)
                {
                    sb.AppendLine(sheet.Name);
                    sb.AppendLine("ListObject Count:" + sheet.ListObjects.Count);
                    if (sheet.ListObjects.Count > 0)
                    {
                        foreach (Excel.ListObject lb in sheet.ListObjects)
                        {
                            sb.AppendLine("ListObjectName:" + lb.Name);
                            foreach (Excel.ListColumn col in lb.ListColumns)
                            {
                                sb.AppendLine("ColumnName:" + col.Name);
                                sb.AppendLine("ColumnXapth:" + col.XPath.Value);    
                            }
                        }
                    }
                }

                MessageBox.Show(sb.ToString());
            }
            catch (Exception ex)
            {
            }
        }
    }
}
